package com.bh.ly.oa.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bh.ly.oa.R;
import com.bh.ly.oa.util.HttpClent;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GoodsSaleDeptDetailDtlAdapter  extends BaseAdapter{
	JSONArray array = null;
	Context context = null;

	public GoodsSaleDeptDetailDtlAdapter(Context contex, String data) {
		this.context = contex;
		this.array = this.getArrayjsonDate(data);
	}

	@Override
	public int getCount() {
		return array.length();
	}

	@Override
	public Object getItem(int position) {
		return array.opt(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GoodSalItem orderItem = null;
		if (convertView == null) {
			orderItem = new GoodSalItem();
			convertView = LayoutInflater.from(context).inflate(R.layout.orderdetail_item, null); 
		} else {
			orderItem = (GoodSalItem) convertView.getTag();
		}
		
		JSONObject jsonObject = (JSONObject) array.opt(position);
		initView(position,convertView,orderItem);
		try {
			orderItem.label.setText(jsonObject.getString("key")+":");
			orderItem.value.setText(jsonObject.getString("value"));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}
	
    private void initView(int position,View convertView,GoodSalItem goodSalItem){
        
    	goodSalItem.label = (TextView) convertView.findViewById(R.id.label);
    	goodSalItem.value = (TextView) convertView.findViewById(R.id.value); 
        convertView.setTag(goodSalItem);
    }
    
	class GoodSalItem {
		TextView label;
		TextView value;
	}
	
	private JSONArray getArrayjsonDate(String data) {
		JSONArray jsonarray=new  JSONArray();
		JSONObject ad=null;
		try {
			JSONObject jb=new JSONObject(data);
			ad=new JSONObject();
			ad.put("key", "商品编码");
			ad.put("value", jb.get("gdsincode"));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "商品名称");
			ad.put("value", jb.get("gdsname"));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "条码");
			String suppliername=jb.get("stripecode").toString();
			ad.put("value",suppliername );
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "基本单位");
			ad.put("value", jb.get("baseunit"));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "部门编码");
			ad.put("value", jb.get("deptcode"));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "部门名称");
			ad.put("value", getStringtext(jb.get("deptname")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "销售数量");
			ad.put("value", getStringtext(jb.get("totalamount")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "含税销售额");
			ad.put("value", getStringtext(jb.get("totalsalemoney")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "含税销售成本");
			ad.put("value", getStringtext(jb.get("totalinmoney")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "无税销售额");
			ad.put("value", getStringtext(jb.get("totalsalemoney_nt")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "无税销售成本");
			ad.put("value",getDatetext( jb.get("totalinmoney_nt")));
			jsonarray.put(ad);
			
			ad=new JSONObject();
			ad.put("key", "销售毛利");
			ad.put("value",getDatetext( jb.get("profitmoney")));
			jsonarray.put(ad);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonarray;
	}
	
	public String getDatetext(Object ob){
		if(ob.toString().equals("null")){
			return "";
		}else{
			return  ob.toString().substring(0, ob.toString().length()-2);
		}
	}
	
	public String getStringtext(Object ob){
		if(ob.toString().equals("null")){
			return "";
		}else{
			return  ob.toString();
		}
	}
}
